package org.bing.zion.core;

class MessageHandlerWrapper extends MessageHandlerChain implements MessageHandler {

	private MessageHandler realHandler;
	// private MessageHandlerChain realChain;
	private MessageHandlerWrapper nextWrapper;
	private MessageHandlerWrapper prevWrapper;

	public MessageHandlerWrapper(MessageHandlerChain chain, MessageHandler handler) {
		// this.realChain = chain;
		this.realHandler = handler;
	}

	public void setNextWrapper(MessageHandlerWrapper next) {
		this.nextWrapper = next;
	}

	public void setPrevWrapper(MessageHandlerWrapper prev) {
		this.prevWrapper = prev;
	}

	@Override
	public MessageHandler next() {
		if (nextWrapper != null) {
			return nextWrapper;
		} else {
			return nextNull();
		}
	}

	@Override
	public MessageHandler prev() {
		if (prevWrapper != null) {
			return prevWrapper;
		} else {
			return prevNull();
		}
	}

	protected MessageHandler nextNull() {
		return new MessageHandler() {
			public void handle(MessageHandlerChain chain, Session session, Object msg) {
				// TODO Auto-generated method stub
			}
		};
	}

	protected MessageHandler prevNull() {
		return new MessageHandler() {
			public void handle(MessageHandlerChain chain, Session session, Object msg) {
				// TODO Auto-generated method stub
			}
		};
	}

	public void handle(MessageHandlerChain chain, Session session, Object msg) {
		if (realHandler != null) {
			realHandler.handle(this, session, msg);
		}
	}
}
